import "./public-path";
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter } from "react-router-dom";

// @ts-ignore
function render(props) {
  const { container } = props;
  // @ts-ignore
  const root = ReactDOM.createRoot(
    container
      ? container.querySelector("#root")
      : document.querySelector("#root")
  );
  root.render(
    <BrowserRouter
      basename={window.__POWERED_BY_QIANKUN__ ? "/reactApp" : "/"}
    >
      <App />
    </BrowserRouter> 
  );
}

export async function bootstrap() {
  console.log("react app bootstraped");
}

// @ts-ignore
export async function mount(props) {
  console.log("props from main framework", props);
  // @ts-ignore
  props.onGlobalStateChange((state, prev) => {
    // state: 变更后的状态; prev 变更前的状态
    console.log(state, prev);
    // setTimeout(() => {
    //   // @ts-ignore
    //   props.setGlobalState({ ...state, age: 20 });
    // }, 2000);
  });

  render(props);
}

// @ts-ignore
export async function unmount(props) {
  const { container } = props;
  // @ts-ignore
  ReactDOM.unmountComponentAtNode(
    container
      ? container.querySelector("#root")
      : document.querySelector("#root")
  );
}

// @ts-ignore
export async function update(props) {
  console.log("update props", props);
}

// @ts-ignore
if (!window.__POWERED_BY_QIANKUN__) {
  render({});
}
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
